package com.tucanstubs.android_wheresmystuff.login;
import java.util.*;

import com.tucanstubs.android_wheresmystuff.items.*;


/**
 * This Class represents a Where'sMyStuff user. Users have a userName, password, email, and account state.
 * They also have a list of all items that they have found and a list of items which they are 
 * searching for. The user's account state determines whether they can access their account. Users
 * accounts can be locked by admin's and locked accounts cannot be accessed by users.
 * @author team 37
 * @version 1.0 2/21/2013
 *
 */
public class User {
	private String userName;
	private String firstName;
	private String lastName;
	private String password;
	private boolean locked;
	private String email;
	private int lockCount;
	private List<Item> items;
	
	/**
	 * Default constructor for User class. Takes in user name, first name, last name
	 * password, and email and sets the corresponding 
	 * user fields. User's account state is unlocked by default, and lockCount starts at 0. User's foundItems and searchItems
	 * list are initialized to empty lists.
	 * 
	 * @param userName user's user name
	 * @param firstName user's first name
	 * @param lastName user's last name
	 * @param password user's password
	 * @param email user's email
	 */
	public User(String userName, String firstName, String lastName, String password, String email){
		this.userName=userName;
		this.firstName=firstName;
		this.lastName=lastName;
		this.password=password;
		this.email=email;
		this.locked=false;
		this.items=new ArrayList<Item>();
		this.lockCount=0;
	}
	/**
	 * Getter for lockCount
	 * @return user's lockCount
	 */
	public int getLockCount() {
		return lockCount;
	}
	/**
	 * Setter for lockCount
	 * @param lockCount user's new lockCount
	 */
	public void setLockCount(int lockCount){
		this.lockCount=lockCount;
	}
	/**
	 * Method to update user's lockCount. Called when user tries to login unsuccessfully. 
	 */
	public void updateLockCount() {
		lockCount=(locked? lockCount: lockCount+1);
		if (lockCount >= 3) {
			locked = true;
		}
	}
	/**
	 * Getter for userName
	 * @return user's userName
	 */
	public String getUserName() {
		return userName;
	}
	/**
	 * Setter for userName
	 * @param userName user's userName
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}
	/**
	 * Getter for password
	 * @return user's password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * Setter for password
	 * @param password user's password
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * Determines is user's account is locked (locked=true not locked=false)
	 * @return boolean indicating whether account is locked or unlocked
	 */
	public boolean isLocked() {
		return locked;
	}
	/**
	 * Sets the user's locked state
	 * @param locked locked state
	 */
	public void setLocked(boolean locked) {
		this.locked = locked;
	}
	/**
	 * Getter for email
	 * @return the user's email
	 */
	public String getEmail() {
		return email;
	}
	/**
	 * Setter for email
	 * @param email user's email
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	/**
	 * Getter for firstName
	 * @return user's firstName
	 */
	public String getFirstName() {
		return firstName;
	}
	/**
	 * Setter for firstName
	 * @param firstName user's firstName
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	/**
	 * Getter for lastName
	 * @return user's lastName
	 */
	public String getLastName() {
		return lastName;
	}
	
	/**
	 * Getter for whether the User is an Admin
	 * @return account status
	 */
	public boolean isAdmin(){
		return false;
	}
	/**
	 * Setter for lastName
	 * @param lastName user's lastName
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public void setItems(ArrayList<Item> items){
		this.items = items;
	}
	/**
	 * Overriding equals method to assert equality based on user information
	 * @param o Object which we are comparing too
	 * @return boolean indicating whether the user and the object are equal
	 */
	public boolean equals(Object o){
		if(o instanceof User){
			User u=(User)o;
			return u.getUserName().equals(userName) && u.getFirstName().equals(firstName) 
					&& u.getLastName().equals(lastName) && u.getEmail().equals(email) 
					&& u.getPassword().equals(password);
		}
		return false;
	}
	
	/**
	 * Overriding hashCode so that we can properly place users in our hash map
	 * @return hashCode of the user's user name
	 */
	public int hashCode(){
		return userName.hashCode();
	}
	
	/**
	 * Adds an item to the user's item list
	 * @param i item to be added
	 */
	public void addItem(Item i){
		items.add(i);
	}
	/**
	 * returns the user's item list
	 * @return user's item list
	 */
	public List<Item> getItems(){
		return items;
	}
	/**
	 * returns a new admin object with the same information as the current user, essentially turning the
	 * user into an admin.
	 * @return new admin object with user info
	 */
	public Admin makeAdmin(){
		return new Admin(userName,firstName,lastName,password,email);
	}
	/**
	 * Returns a string list of the user's info
	 * @return a string list of the user's info
	 */
	public List<String> toList(){
		List<String> list = new ArrayList<String>();
		list.add("User Name: " + userName);
		list.add("First Name: " + firstName);
		list.add("Last Name: " + lastName);
		list.add(locked ? "Status: Locked": "Status: Unlocked");
		list.add("Current lock count: " + lockCount);
		list.add(isAdmin() ? "Admin" : "User");
		return list;
	}
	

}



